/**
 * Copyright (C) 2012-2013 Dystopian Dreams
 * @author Danielle Obmann <alisiel.isalos@gmail.com>
 * 
 * 
 * This file is part of Grue.

    Grue is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Grue is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Grue.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.util.ArrayList; 
import javax.swing.DefaultListModel;
import java.util.*;

public class Encounter 
{
    Party theParty;
    DefaultListModel theEncounterList;
    ArrayList<Character> theEncounter;
    
    public Encounter()
    {
        theEncounter=new ArrayList();
        theEncounterList=new DefaultListModel();        
    }
    
    void updateParty(Party argParty)
    {
        theParty=argParty;
        theEncounter.clear();
        theEncounterList.clear();
        
        for(Iterator<Character> i=theParty.getParty().iterator(); i.hasNext();)
        {
            Character theCharacter=i.next();
            theCharacter.setInit(0);
            theEncounter.add(theCharacter);
            theEncounterList.addElement(theCharacter.getName());
        }
    }
    
//    void addEncounter()
//    {
//    This requires a storage medium, should be a database.
//    Research mysql set up for this use.
//    This also requires that there be a method to create an encounter to add
//    }
    
    /**
     * 
     * @return 
     */
    DefaultListModel getList()
    {
        return theEncounterList;
    }
    
    void rollInitiative()
    {
        Boolean swapped=true;
        while(swapped.booleanValue())
        {
            swapped=false;
            for(int i=1;i<theEncounter.size();i++)
            {
                Character lower=theEncounter.get(i-1);
                Character upper=theEncounter.get(i);

                if (lower.getInit()<upper.getInit())
                {
                    theEncounter.set(i, lower);
                    theEncounter.set(i-1, upper);
                    swapped=true;  
                }
            }
        }
        theEncounterList.clear();
        for(Iterator<Character> i=theEncounter.iterator(); i.hasNext();)
        {
            Character theCharacter=i.next();
            theEncounterList.addElement(theCharacter.getName());
        }
    }
}
